import com.css.sword.knowing.search.SearchField;
import com.css.sword.knowing.search.SearchInput;
import com.css.sword.knowing.search.SearchResult;
import com.css.sword.knowing.search.Searcher;
import com.css.sword.knowing.search.comm.SearchConst;
import com.css.sword.knowing.search.exception.ArgException;
import org.elasticsearch.ElasticsearchException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class SearchDemo {
    
    private static String getString(String fPath)
    {
        String str = "";
      //  File f = new File("e:/temp/cldid.txt");
        File f = new File(fPath);
        try {
            FileInputStream fip = new FileInputStream(f);
        

            InputStreamReader reader = new InputStreamReader(fip, "UTF-8");
    
            StringBuffer sb = new StringBuffer();
            while (reader.ready()) {
                sb.append((char) reader.read());
            }
            reader.close();
    
            fip.close();
            str = sb.toString();
        } catch (IOException e) {
           
            e.printStackTrace();
        }
        
        return str;
    }
    public static void search() {
        Searcher.Debug = true;

        try {
         //   String str = CssFileUtil.readString("test2.json");
        //    System.out.println(str);
         //   List<SearchField> where = QueryBuilder.create(str);

            
            
            SearchInput si = new SearchInput();

            //si.setIndexName("gwgl_zh_cld_test");
            si.setIndexName("zh_dbgz");

            si.setTypeNames(new String[] { "all" });

            String[] ids = getString("e:/temp/cldid10.txt").split(",");
            List<SearchField> fields = new ArrayList<SearchField>();
//            for(int i=0; i<ids.length; i++)
//            {
//                SearchField f = new SearchField();
//                f.setField("cldid");
//                f.setSearchValue(ids[i]);
//                f.setMethod(SearchConst.SEARCH_METHOD_TERM);
//                f.setOperator(SearchConst.SEARCH_OPERATOR_SHOULD);
//                
//                fields.add(f);
//            }
            
            List<SearchField> where = new ArrayList<SearchField>();
            SearchField field = new SearchField();
         //   field.setChildrenField(fields);
            field.setOperator(SearchConst.SEARCH_OPERATOR_MUST);
            
            field.setField("cldid");
            field.setSearchValue(ids);
            field.setMethod(SearchConst.SEARCH_METHOD_TERMS);
            
            where.add(field);
            
            si.setSearchFields(where);
            
            si.setFrom(0);
            si.setSize(10);
            
            List<SearchInput> sis = new ArrayList<SearchInput>();
            sis.add(si);
            
            long tm1 = System.currentTimeMillis();
            // 检索索引库
            List<SearchResult> rss = Searcher.multiIndexSearch(sis);
            long tm2 = System.currentTimeMillis();
            
            // 打印检索结果
            System.out.println(rss.get(0).toJson());

            // 打开Searcher.debug标记后可以显示出检索条件
//            System.out.println(rss.get(0).getQuery());

            System.out.println(ids.length);
            System.out.println("所用时间：" + (tm2 - tm1));

        } catch (ArgException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) throws IOException, ElasticsearchException, ArgException {
        //Searcher.getTransportClient();
        
        search();
    }
}
